package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.domain.BizAccount;
import com.ruoyi.business.domain.BizAccountDetail;
import com.ruoyi.business.mapper.BizAccountMapper;
import com.ruoyi.business.service.IBizAccountService;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.utils.DictUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.BizCashInfoMapper;
import com.ruoyi.business.domain.BizCashInfo;
import com.ruoyi.business.service.IBizCashInfoService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 兑现申请记录Service业务层处理
 *
 * @author ruoyi
 * @date 2020-09-23
 */
@Service
public class BizCashInfoServiceImpl implements IBizCashInfoService
{
    @Resource
    private BizCashInfoMapper bizCashInfoMapper;

    @Resource
    private IBizAccountService bizAccountService;

    @Resource
    private BizAccountMapper bizAccountMapper;

    /**
     * 查询兑现申请记录
     *
     * @param id 兑现申请记录ID
     * @return 兑现申请记录
     */
    @Override
    public BizCashInfo selectBizCashInfoById(Long id)
    {
        return bizCashInfoMapper.selectBizCashInfoById(id);
    }

    /**
     * 查询兑现申请记录列表
     *
     * @param bizCashInfo 兑现申请记录
     * @return 兑现申请记录
     */
    @Override
    public List<BizCashInfo> selectBizCashInfoList(BizCashInfo bizCashInfo)
    {
        return bizCashInfoMapper.selectBizCashInfoList(bizCashInfo);
    }

    /**
     * 查询当天申请金额
     *
     * @return
     */
    @Override
    public Long selectTodayMoney()
    {
        return bizCashInfoMapper.selectTodayMoney();
    }

    /**
     * 新增兑现申请记录
     *
     * @param bizCashInfo 兑现申请记录
     * @return 结果
     */
    @Override
    public int insertBizCashInfo(BizCashInfo bizCashInfo)
    {
        bizCashInfo.setCreateTime(DateUtils.getNowDate());
        return bizCashInfoMapper.insertBizCashInfo(bizCashInfo);
    }

    /**
     * 修改兑现申请记录
     *
     * @param bizCashInfo 兑现申请记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateBizCashInfo(BizCashInfo bizCashInfo)
    {
        bizCashInfo.setUpdateTime(DateUtils.getNowDate());
        int status = bizCashInfo.getStatus();
        int row = bizCashInfoMapper.updateBizCashInfo(bizCashInfo);
        //驳回退款
        if (row > 0 && status == BizCashInfo.CASH_INFO_STATUS_DENY) {
            int type = bizCashInfo.getAccountType();
            long money = bizCashInfo.getDrawMoney();
            if (type == BizAccount.DOU_FIELD) {
                float fieldRate = Float.parseFloat(DictUtils.getDictLabel("busi_cashdraw_set", "1")) / 100;
                money /= fieldRate;
            }
            //返还福豆账户
            bizAccountService.accountChange(bizCashInfo.getMemberId(), type, BizAccountDetail.DOU_DETAIL_TYPE_CHARGE, money, "", BizAccountDetail.DOU_DESC_DENY_RETURN);
        }
        return row;
    }

    /**
     * 批量通过兑现申请记录
     *
     * @param ids 需要通过的数据ID
     * @return 结果
     */
    @Override
    public int updateStatusAll(String[] ids)
    {
        return bizCashInfoMapper.updateStatusAll(ids);
    }

    /**
     * 删除兑现申请记录信息
     *
     * @param id 兑现申请记录ID
     * @return 结果
     */
    @Override
    public int deleteBizCashInfoById(Long id)
    {
        return bizCashInfoMapper.deleteBizCashInfoById(id);
    }

    /**
     * 添加兑现申请记录
     *
     * @param bizCashInfo 兑现申请记录
     * @return 结果
     */
    @Override
    @Transactional
    public AjaxResult addCashInfo(BizCashInfo bizCashInfo) {
        Long memberId = bizCashInfo.getMemberId();
        int type = bizCashInfo.getAccountType();
        BizAccount bizAccount = new BizAccount();
        bizAccount.setMemberId(memberId);
        bizAccount.setAccountType(type);
        List<BizAccount> accountList = bizAccountMapper.selectBizAccountList(bizAccount);
        if (accountList == null || accountList.size() == 0) {
            return AjaxResult.warn("用户数据有误,请联系管理员");
        }
        bizCashInfo.setAccountId(accountList.get(0).getId());
        long reduceCash = - bizCashInfo.getDrawMoney();
        if (type == BizAccount.DOU_FIELD) {
            float fieldRate = Float.parseFloat(DictUtils.getDictLabel("busi_cashdraw_set", "1")) / 100;
            reduceCash /= fieldRate;
        }
        //减去福豆账户
        boolean result = bizAccountService.accountChange(memberId, type, BizAccountDetail.DOU_DETAIL_TYPE_DRAW, reduceCash, "", BizAccountDetail.DOU_DESC_DRAW);
        if (!result) {
            return AjaxResult.warn("扣款失败,请联系管理员");
        }
        insertBizCashInfo(bizCashInfo);
        return AjaxResult.success();
    }
}
